package com.evenvi.MapReduce;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Writable;

import java.io.*;

public class WritableIO {
    public static void main(String[] args) throws IOException{
        IntWritable writable = new IntWritable();
        writable.set(1331321);
        byte[] bytes = serialize(writable);

        for (int i=0; i<bytes.length; i++){
            System.out.print(bytes[i]);
        }

        IntWritable writable2 = new IntWritable();
        deserialize(writable2, bytes);
        System.out.println(writable2.get());
    }


    public static byte[] serialize(Writable writable) throws IOException{
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(out);
        writable.write(dataOut);
        dataOut.close();
        return out.toByteArray();
    }

    public static byte[] deserialize(Writable writable, byte[] bytes) throws IOException{
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        DataInputStream dataIn = new DataInputStream(in);
        writable.readFields(dataIn);
        dataIn.close();
        return bytes;
    }




}
